﻿Imports System.Linq
'Imports System.Collections.Specialized
Imports System.DirectoryServices
'Imports System.DirectoryServices.ActiveDirectory

Public Class Form1
    Dim strFilterName As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each item As String In My.Settings.colFilterKey
            lstVisualFilter.Items.Add(item.ToString)
        Next
        Dim dt As Date = Now
        dt = dt.AddDays(-180)
        MonthCalendar1.SelectionStart = (dt)
        MonthCalendar1.SelectionEnd = (dt)

    End Sub




    Private Sub BtnExecuteClick(sender As Object, e As EventArgs) Handles btnExecute.Click
        Dim connectionPrefix As String = "LDAP://"
        'Code assumes your running application from machine on the domain you want to search otherwise change objectLdapPath to your domain
        Dim objectLdapPath As String = Environment.UserDomainName
        'Dim username As String = "Nothing"
        'Dim password As String = "Nothing"
        Dim lngPwdLstSt As Long = ADServices.ConvertTo64bit(MonthCalendar1.SelectionStart)
        Dim objSearchComp As New DirectorySearcher
        Dim directoryObject As New DirectoryEntry(connectionPrefix & objectLdapPath, Nothing, Nothing)
        objSearchComp.SearchRoot = directoryObject
        'objSearchComp.Filter = "(&(objectCategory=Computer)(objectClass=Computer)(samAccountType:1.2.840.113556.1.4.803:=805306369)(|(!(lastLogon=*))(lastLogon<=129636059360000000)))"
        objSearchComp.Filter = "(&(|(pwdlastset<=" & lngPwdLstSt & ")(!(pwdlastset=*)))(samaccounttype:1.2.840.113556.1.4.803:=805306369)(objectclass=Computer)(objectcategory=Computer))"
        Dim objResults As SearchResultCollection = objSearchComp.FindAll()

        ' Iterate through each SearchResult in the SearchResultCollection.
        Dim searchResult As SearchResult
        'Boolean variable for RegEx function
        Dim boolVal As Boolean
        For Each searchResult In objResults
            Dim strFuglyFilter As String
            strFuglyFilter = (searchResult.Properties("distinguishedName")(0))
            boolVal = ADServices.BuildRegEx(strFuglyFilter)
            If boolVal = False Then
                lstFilter.Items.Add((searchResult.Properties("name")(0)))
                lstDN.Items.Add((searchResult.Properties("distinguishedName")(0)))
            Else
                Console.WriteLine("Value found in string!")
            End If


        Next
        lblTotal.Text = lstDN.Items.Count
    End Sub

    Private Sub BtnAddFilterClick(sender As Object, e As EventArgs) Handles btnAddFilter.Click


        If txtFilterName.TextLength > 2 Then
            lstVisualFilter.Items.Add(txtFilterName.Text)
            My.Settings.colFilterKey.Add(txtFilterName.Text)
            txtFilterName.Text = ""
        Else
            MsgBox("Search filter to short")
        End If


    End Sub


    Private Sub BtnDelFilterClick(sender As Object, e As EventArgs) Handles btnDelFilter.Click

        Dim intLstVisFilIndex As Integer
        intLstVisFilIndex = lstVisualFilter.SelectedIndex
        lstVisualFilter.Items.Remove(lstVisualFilter.SelectedItem())
        My.Settings.colFilterKey.RemoveAt(intLstVisFilIndex)

    End Sub

    Private Sub BtnClearlstFilClick(sender As Object, e As EventArgs) Handles btnClearlstFil.Click
        lstDN.Items.Clear()
        lstFilter.Items.Clear()
    End Sub

    Private Sub BtnExportClick(sender As Object, e As EventArgs) Handles btnExport.Click
        Dim objExcel
        objExcel = CreateObject("Excel.Application")
        objExcel.Workbooks.Add()
        Dim intRow As Integer = 2
        For i = 0 To lstFilter.Items.Count - 1
            objExcel.Cells(intRow, 1).Value = lstFilter.Items.Item(i)
            objExcel.Cells(intRow, 2).Value = lstDN.Items.Item(i)
            intRow = intRow + 1
        Next i
        objExcel.Visible = True

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each domain In ADServices.EnumDomains
            MsgBox(Domain)
        Next
        MsgBox(Environment.UserDomainName)
        'MsgBox(ADServices.ConvertTo64bit(MonthCalendar1.SelectionStart))
    End Sub
End Class
